package com.hkfg.service.impl;

import com.hkfg.dao.UserDao;
import com.hkfg.domain.User;
import com.hkfg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public int logInCheck(String username, String password) {

        User user = userDao.selectByUsername(username);

//        System.out.println(user);
//        System.out.println(password);
        if(user == null){
            return 0;      // 用户名不存在
        }else if(!user.getPassword().equals(password)){     // 这里用==有问题
            return -1;      // 用户名存在，密码不正确
        }else{
            return user.getId();      // 登陆成功
        }

    }

    @Override
    public int register(String username, String password) {

        if(userDao.selectByUsername(username) != null){
            return -2;      // 用户已存在
        }else{
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            userDao.add(user);
            return userDao.selectByUsername(username).getId();     // 获得id
        }
    }

    @Override
    public int getUserId(String username) {
        return userDao.selectByUsername(username).getId();
    }

    @Override
    public int modifyWelcome(int userId, String description) {
        return userDao.modifyWelcome(userId, description);
    }

    public User getUserById(int id){
        return userDao.selectById(id);
    }




}
